package com.zjd.association.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.entity.TeamTypes;
import com.zjd.association.entity.Teams;
import com.zjd.association.mapper.TeamTypesMapper;
import com.zjd.association.mapper.TeamsMapper;
import com.zjd.association.service.TeamTypesService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动类型 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class TeamTypesServiceImpl implements TeamTypesService {

    private final TeamTypesMapper teamTypesMapper;

    private final TeamsMapper teamsMapper;

    @Override
    public void add(TeamTypes teamTypes) {
        teamTypesMapper.insert(teamTypes);
    }

    @Override
    public void update(TeamTypes teamTypes) {
        teamTypesMapper.updateById(teamTypes);
    }

    @Override
    public void delete(TeamTypes teamTypes) {
        teamTypesMapper.deleteById(teamTypes);
    }

    /**
     * 检查指定的活动类型是否可以删除
     * 如果存在关联活动不能删除
     *
     * @param typeId 活动ID
     * @return 结果
     */
    @Override
    public Boolean isRemove(String typeId) {
        QueryWrapper<Teams> qw = new QueryWrapper<>();
        qw.eq("type_id", typeId);

        return teamsMapper.selectCount(qw) <= 0;
    }

    @Override
    public TeamTypes getOne(String id) {
        QueryWrapper<TeamTypes> qw = new QueryWrapper<>();
        qw.orderByDesc("create_time");

        return teamTypesMapper.selectById(id);
    }

    /**
     * 获取全部的活动类型信息
     *
     * @return 团队类型
     */
    @Override
    public List<TeamTypes> getAll() {
        return teamTypesMapper.selectList(null);
    }

    /**
     * 分页查询活动类型信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param teamTypes 模糊查询条件
     * @return 团队类型
     */
    @Override
    public PageWrapper getPageInfo(Long pageIndex, Long pageSize, TeamTypes teamTypes) {
        QueryWrapper<TeamTypes> qw = new QueryWrapper<>();

        if (StrUtil.isNotBlank(teamTypes.getName())) {
            qw.like("name", teamTypes.getName());
        }

        qw.orderByDesc("create_time");

        Page<TeamTypes> page
                = teamTypesMapper.selectPage(new Page<>(pageIndex, pageSize), qw);

        return parsePage(page);
    }

    /**
     * 解析分页包装
     *
     * @param p 分页数据
     * @return 分页包装
     */
    public PageWrapper parsePage(Page<TeamTypes> p) {
        List<Map<String, Object>> res = new ArrayList<>();

        for (TeamTypes teamTypes : p.getRecords()) {
            Map<String, Object> temp = new HashMap<>();
            temp.put("id", teamTypes.getId());
            temp.put("name", teamTypes.getName());
            temp.put("createTime", teamTypes.getCreateTime());
            res.add(temp);
        }

        return new PageWrapper(p.getCurrent(), p.getSize(), p.getTotal(), res);
    }
}